import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import okhttp3.*;
import sun.misc.BASE64Encoder;


class Demo {
    public static String calcAuthorization(String source, String secretId, String secretKey, String datetime)
            throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
        String signStr = "x-date: " + datetime + "\n" + "x-source: " + source;
        Mac mac = Mac.getInstance("HmacSHA1");
        Key sKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), mac.getAlgorithm());
        mac.init(sKey);
        byte[] hash = mac.doFinal(signStr.getBytes("UTF-8"));
        String sig = new BASE64Encoder().encode(hash);

        String auth = "hmac id=\"" + secretId + "\", algorithm=\"hmac-sha1\", headers=\"x-date x-source\", signature=\"" + sig + "\"";
        return auth;
    }

    public static String urlencode(Map<?, ?> map) throws UnsupportedEncodingException {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            if (sb.length() > 0) {
                sb.append("&");
            }
            sb.append(String.format("%s=%s",
                    URLEncoder.encode(entry.getKey().toString(), "UTF-8"),
                    URLEncoder.encode(entry.getValue().toString(), "UTF-8")
            ));
        }
        return sb.toString();
    }

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException {

    //云市场分配的密钥Id
        String secretId = "";
        //云市场分配的密钥Key
        String secretKey = "";
        String source = "market";

        Calendar cd = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        String datetime = sdf.format(cd.getTime());
        // 签名
        String auth = calcAuthorization(source, secretId, secretKey, datetime);


        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();


        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("multipartFile","dingdan.jpg",
                        RequestBody.create(MediaType.parse("application/octet-stream"),
                                new File("dingdan.jpg")))
                .build();

        Request request = new Request.Builder()
                .url("https://service-6kzl8hr5-1258478321.sh.apigw.tencentcs.com/release/ocr/onlyText")
                .method("POST", body)
                .addHeader("X-Source", source)
                .addHeader("X-Date", datetime)
                .addHeader("Authorization", auth)
                .build();
        //add header
//        request.newBuilder().addHeader("X-Source", source);
//        request.newBuilder().addHeader("X-Date", datetime);
//        request.newBuilder().addHeader("Authorization", auth);

        Response response = client.newCall(request).execute();
        //get response json text
        String result = response.body().string();
        System.out.println(result);
    }
}